package com.huadi.ssm.Controller;

import com.huadi.ssm.Service.ApplyrecordService;
import com.huadi.ssm.Service.EvidenceService;
import com.huadi.ssm.dao.ApplyrecordMapper;
import com.huadi.ssm.po.Applyrecord;
import com.huadi.ssm.po.Evidence;
import com.huadi.ssm.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/applyrecord")
public class ApplyreordController {
    @Autowired
    private ApplyrecordService applyrecordService;

    @Resource
    private EvidenceService evidenceService;

    //1:司法人员查询个人所有的申请并返回到页面
    @RequestMapping("/allApply")
    private String getAllreply(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");

        List<Applyrecord> applyrecord = applyrecordService.findAllApplyrecordForONE(user.getUserId());

        model.addAttribute("applyrecord", applyrecord);

        return "applyrecord";
    }





    //2:跳转到增加申请页面

    @RequestMapping("/toaddapplyrecord")
    public String toaddapplyrecord() {
        return "addAR";
    }

    //3:完成增加操作，并返回到所有申请的页面
    @RequestMapping("/addAR")
    public String addUser(String evidenceId, String evidenceState, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (evidenceService.findEvidenceByUserId(evidenceId) != null) {
            applyrecordService.addApplyrecord(evidenceId, evidenceState, user.getUserId(), user.getUsername());
            return "redirect:/applyrecord/allApply";
        } else {
            return "addAR";
        }
    }


//
//    //4:删除申请
//    @RequestMapping("/deleteAR")
//    public String deleteAR(int evidenceApplyId){
//        applyrecordService.deleteApplyrecordById(evidenceApplyId);
//        return "redirect:/applyrecord/allApply";
//    }


    //:中心人员查看所有的物证申请
    @RequestMapping("/allARforCentral")
    public String allARforCentral(Model model) {
        List<Applyrecord> applyrecord = applyrecordService.findAllApplyrecord();
        model.addAttribute("applyrecord", applyrecord);
        return "aprecordforCentral";
    }

    //:跳转到审核申请页面
    @RequestMapping("/tocheckAR")
    public String tocheckAR(){return "checkApply";}

    //6:审核申请，审核之后自动修改数据库相关物证
    @RequestMapping("/checkAR")
    public String checkAR(int evidenceApplyId, String judgerId, String judgerName, String judge, String judgeReason,HttpSession session) {
        User user= (User) session.getAttribute("user");
        applyrecordService.checkApply(evidenceApplyId, user.getUserId(), user.getUsername(), judge, judgeReason);
        if(judge.equals("通过")){
            Applyrecord applyrecord = applyrecordService.findApplyrecordById(evidenceApplyId);
            evidenceService.FinishCheck(applyrecord.getEvidenceId());
        }

        return "redirect:/applyrecord/allARforCentral";

    }
}
